package com.ysw.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.ysw.bean.TransDemo;
import com.ysw.service.TransDemoService;
import com.ysw.mapper.TransDemoMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CyclicBarrier;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【trans_demo】的数据库操作Service实现
 * @createDate 2023-04-27 14:39:03
 */
@Service
public class TransDemoServiceImpl extends ServiceImpl<TransDemoMapper, TransDemo>
        implements TransDemoService {
    private TransactionTemplate transactionTemplate;

    public TransDemoServiceImpl(PlatformTransactionManager platformTransactionManager) {
        this.transactionTemplate = new TransactionTemplate(platformTransactionManager);
    }

    @Override
    public void testThreads() {
        ArrayList<TransDemo> transDemos = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            transDemos.add(TransDemo.builder().name("i" + 1).version(i % 2).build());
        }
        List<List<TransDemo>> partition = Lists.partition(transDemos, 50);
        List<Runnable> tasks = partition.stream().map(e -> (Runnable) () -> transDemoMapper.insertBatch(e)).collect(Collectors.toList());
        Coordinator coordinator = new Coordinator(transactionTemplate, tasks.toArray(new Runnable[]{}));
        coordinator.begin();
    }

    @Transactional
    @Override
    public void testTransactionSynchronizationManager() {
        TransactionSynchronizationUtil.doAfterTransaction(()-> System.out.println("事务已经完成了2"));
        baseMapper.insert(TransDemo.builder().name("你好1").build());
        baseMapper.updateById(TransDemo.builder().id(1).name("王5").build());
    }
    /**
     * 编程式事务
     */
    @Override
    public void testTrans() {
        transactionTemplate.executeWithoutResult(transactionStatus -> {
            try {
                TransactionSynchronizationUtil.doAfterTransaction(()-> System.out.println("事务已经完成了1"));
                baseMapper.insert(TransDemo.builder().name("你好2").build());
                baseMapper.updateById(TransDemo.builder().id(1).name("王5").build());
            } catch (Exception e) {
                transactionStatus.setRollbackOnly();
            }
        });
    }



    @Autowired
    TransDemoMapper transDemoMapper;
}


